<script setup>
import { ref } from 'vue'
import { User } from '@element-plus/icons-vue'
import { userLoginSerivce, userResgisterService } from '@/api/login/user.js'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
// 创建路由对象
const router = useRouter()
// 控制注册还是登录的状态
const isRegister = ref(false)
// 表单绑定的数据
const formModel = ref({
  username: '',
  password: '',
  rePassword: ''
})
// 表单项校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^\S{3,16}$/,
      message: '必须是3到12位字母或数字',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{3,12}$/,
      message: '必须是3到12位字母或数字',
      trigger: 'blur'
    }
  ],
  rePassword: [
    { required: true, message: '请输入确定密码', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{3,12}$/,
      message: '必须是3到12位字母或数字',
      trigger: 'blur'
    },
    {
      validator: (rule, value, ck) => {
        if (value !== formModel.value.password) {
          ck(new Error('两次密码不一致'))
        } else {
          ck()
        }
      },
      trigger: 'blur'
    }
  ]
}
// el-form 组件对象
const formRef = ref()
// 获取用户仓库对象
const userStore = useUserStore()
// 发送请求
const onSubmit = async () => {
  // 预校验
  await formRef.value.validate()
  if (isRegister.value) {
    // 注册账号
    await userResgisterService(formModel.value)
  } else {
    // 登录
    const res = await userLoginSerivce(formModel.value)
    userStore.setToken(res.data)
  }
  ElMessage.success(isRegister.value ? '注册成功' : '登录成功')
  router.push('/admin')
}

const onSwitch = () => {
  isRegister.value = !isRegister.value
  formModel.value = { username: '', password: '', rePassword: '' }
}

defineOptions({
  name: 'LoginPage'
})
</script>
<template>
  <div class="login-page">
    <el-row>
      <el-col :span="8"></el-col>
      <el-col :span="6" class="login-form">
        <el-form
          ref="formRef"
          style="width: 250px"
          :model="formModel"
          status-icon
          :rules="rules"
          label-width="auto"
          class="login-form"
        >
          <el-form-item>
            <h4 style="text-align: center; width: 100%; color: aliceblue">
              {{ isRegister ? '注册账号' : '登录' }}
            </h4>
          </el-form-item>
          <el-form-item prop="username">
            <el-input
              v-model="formModel.username"
              placeholder="账号"
              :icon="User"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="formModel.password"
              type="password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="rePassword" v-if="isRegister">
            <el-input
              v-model="formModel.rePassword"
              type="password"
              placeholder="请输入确定密码"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="onSubmit">{{
              isRegister ? '注册' : '登录'
            }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" @click="onSwitch">{{
              isRegister ? '去登录' : '注册账号'
            }}</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="less">
.login-page {
  height: 100%;
  background: url(../../assets/imgs/bg00.jpg) no-repeat center/cover;
  .login-form {
    margin-top: 80px;
    background: rgba(255, 255, 255, 0.5);
    text-align: center;
    border-radius: 10px;
    padding: 5px 20px;
    .el-input {
      background-color: transparent;
      color: #fff;
      outline: #fff;
      border-color: #fff;
    }
  }
}
</style>
@/api/login/user.js
